#!/usr/bin/python  
from socket import *  
import struct,os,time,sys  

from ntp_server_lib import get_random_ntp_server

# print get_random_ntp_server()

time_server = (get_random_ntp_server(), 123)  

TIME1970 = 2208988800L      # Thanks to F.Lundh  

def get_ntp_time():
    client = socket( AF_INET, SOCK_DGRAM )  
    data = '\x1b' + 47 * '\0'  
    client.sendto(data, time_server)
    try:
        data, address = client.recvfrom( 1024 )
    except:
        return -1
    if data:  
        print 'Response received from', address,'\n'  
        t = struct.unpack( '!12I', data )[10]  
        if t == 0:  
            return -1  
        return t - TIME1970
    else:  
        return -1

